package StudentController;

import Student.Student;
import StudentService.StudentService;

import java.util.Scanner;
import StudentService.StudentServiceInterface;
import exception.LoginExcepiton;


public class StudentController {
    StudentServiceInterface studentServiceInterface = new StudentService();
    Scanner sc = new Scanner(System.in);

    public void zhuce() {
        String name;
        while (true) {
            System.out.println("请输入姓名");
            name = sc.next();
            boolean flag = studentServiceInterface.Exit(name);
            if (flag == false) {
                System.out.println("姓名重复，请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入密码");
        String password = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入学科");
        String subject = sc.next();
        Student student = new Student(name, password, age, subject);
        studentServiceInterface.zhuce(student);
        System.out.println("注册成功！");
    }

    public void findAllStudent() {
        studentServiceInterface.findAllStudent();

    }

    public void login() {
        String name;
        System.out.println("请输入姓名");
        name = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        while (true) {
            System.out.println("请输入验证码");
            StringBuilder yanzhengma = studentServiceInterface.yanzhengma();
            System.out.println(yanzhengma);
            String yanzhen = sc.next();
            boolean yan = studentServiceInterface.yanzhen(yanzhengma,yanzhen);
            if (yan == false) {
                System.out.println("验证码错误，请重新输入验证码");
            }else {
                break;
            }
        }
        Student student = new Student(name,password);
        try {
            studentServiceInterface.login(student);
        } catch (LoginExcepiton L) {
            System.out.println(L.getMessage());
        }
    }

    public void updatePassWord() {
        System.out.println("请输入姓名");
        String name = sc.next();
        boolean flag=studentServiceInterface.Exit(name);
        if (flag==true){
            System.out.println("数据不存在，请添加后再查询");
            return;
        }
        System.out.println("请输入你要修改的密码");
        String newPassWord = sc.next();
        boolean flag1 =studentServiceInterface.updatePassWord(name,newPassWord);
        if (flag1 == true){
            System.out.println("修改成功！");
        }
    }
}
